﻿Namespace EIBData.DPT
    Public MustInherit Class EIBDataDPT_16_Base
        Inherits EIBDataDPT_Base

        Protected _valueBase As Byte() = Nothing


        Sub New(data As Byte())
            If Not CanEncode(data) Then Throw New EIB.Exceptions.EIBDataDecodeException("")

            MyBase._data = data
            _valueBase = New Byte(13) {}
            For i = 0 To 13
                _valueBase(i) = data(i + 1)
            Next
        End Sub

        Sub New(value As Byte(), dummy As Boolean)
            If value.Length > 14 Then Throw New EIB.Exceptions.EIBDataEncodeException("")

            _valueBase = New Byte(13) {}
            Array.Copy(value, _valueBase, value.Length)

            MyBase._data = New Byte() {0,
                                      _valueBase(0),
                                      _valueBase(1),
                                      _valueBase(2),
                                      _valueBase(3),
                                      _valueBase(4),
                                      _valueBase(5),
                                      _valueBase(6),
                                      _valueBase(7),
                                      _valueBase(8),
                                      _valueBase(9),
                                      _valueBase(10),
                                      _valueBase(11),
                                      _valueBase(12),
                                      _valueBase(13)
                                     }
        End Sub


        Public Overrides Function CanEncode(data() As Byte) As Boolean
            Return data IsNot Nothing AndAlso data.Length = DPT_OctetLength
        End Function

        Public Overrides Function ToString() As String
            Return EIB.Helper.ConvertByteArrayToHexString(_valueBase)
        End Function

        Public Overrides Function ToObject() As Object()
            Return New Object() {_valueBase}
        End Function

        Public Overrides ReadOnly Property ObjectType As System.Type()
            Get
                Return New Type() {GetType(Byte())}
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_BaseName As String
            Get
                Return "A112"
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_OctetLength As Byte
            Get
                Return 15
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_Name As String
            Get
                Return "DPT_16"
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_Number As String
            Get
                Return "16.xxx"
            End Get
        End Property
    End Class
End Namespace